{
 "cells": [
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "# 多维数组的组合与拆分\n",
    "|操作| 函数                 |说明|\n",
    "|----|--------------------|---|\n",
    "|垂直组合/拆分| vstack()/vsplit()  | 沿着0轴（行方向）操作|\n",
    "|水平组合/拆分|hstack() / hsplit()|沿第1轴（列方向）操作|\n",
    "|深度组合/拆分|dstack() /dsplit()|沿着第2轴（深度方向）操作，用于3D数组|\n",
    "|通用函数|np.concatenate() / np.split()|通过 axis参数指定方向|\n"
   ],
   "id": "ba5c0f6a7fde6bee"
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "## 1. 垂直方向操作",
   "id": "3799fd4b757092eb"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-26T03:10:39.884836Z",
     "start_time": "2025-08-26T03:10:39.872177Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import numpy as np\n",
    "arr1 = np.arange(1,7).reshape(2,3)\n",
    "arr2 = np.arange(7,13).reshape(2,3)\n",
    "\n",
    "print(arr1)\n",
    "print('-' * 30)\n",
    "print(arr2)\n",
    "\n",
    "print('=' * 30)\n",
    "# 垂直方向完成组合操作，生成新数组\n",
    "arr3 = np.vstack((arr1,arr2))\n",
    "print(arr3)\n",
    "print('$' * 30)\n",
    "# 垂直方向完成拆分操作，生成新数组\n",
    "a,b,c,d = np.vsplit(arr3,4)\n",
    "print(a)\n",
    "print('=' * 30)\n",
    "print(b)\n",
    "print('=' * 30)\n",
    "print(c)\n",
    "print('=' * 30)\n",
    "print(d)"
   ],
   "id": "b7af5e16a7ad9c7f",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 2 3]\n",
      " [4 5 6]]\n",
      "------------------------------\n",
      "[[ 7  8  9]\n",
      " [10 11 12]]\n",
      "==============================\n",
      "[[ 1  2  3]\n",
      " [ 4  5  6]\n",
      " [ 7  8  9]\n",
      " [10 11 12]]\n",
      "$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n",
      "[[1 2 3]]\n",
      "==============================\n",
      "[[4 5 6]]\n",
      "==============================\n",
      "[[7 8 9]]\n",
      "==============================\n",
      "[[10 11 12]]\n"
     ]
    }
   ],
   "execution_count": 9
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "## 2. 水平方向操作",
   "id": "a11c46a0edb454f6"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-26T03:13:27.132660Z",
     "start_time": "2025-08-26T03:13:27.120496Z"
    }
   },
   "cell_type": "code",
   "source": [
    "print(arr1)\n",
    "print('=' * 30)\n",
    "print(arr2)\n",
    "\n",
    "print('=' * 30)\n",
    "# 水平方向合并\n",
    "arr4 = np.hstack((arr1,arr2))\n",
    "print(arr4)\n",
    "print('$' * 30)\n",
    "# 水平方向拆分\n",
    "e,f,g = np.hsplit(arr4,3)\n",
    "print(e)\n",
    "print(\"=\" * 30)\n",
    "print(f)\n",
    "print(\"=\" * 30)\n",
    "print(g)"
   ],
   "id": "ecfb98776b7b623a",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 2 3]\n",
      " [4 5 6]]\n",
      "==============================\n",
      "[[ 7  8  9]\n",
      " [10 11 12]]\n",
      "==============================\n",
      "[[ 1  2  3  7  8  9]\n",
      " [ 4  5  6 10 11 12]]\n",
      "$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n",
      "[[1 2]\n",
      " [4 5]]\n",
      "==============================\n",
      "[[ 3  7]\n",
      " [ 6 10]]\n",
      "==============================\n",
      "[[ 8  9]\n",
      " [11 12]]\n"
     ]
    }
   ],
   "execution_count": 12
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "## 3. 纵深方向操作(3维)",
   "id": "3ec5eae606d308"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-26T03:21:26.350406Z",
     "start_time": "2025-08-26T03:21:26.340742Z"
    }
   },
   "cell_type": "code",
   "source": [
    "print(arr1)\n",
    "print('=' * 30)\n",
    "print(arr2)\n",
    "print('$' * 30)\n",
    "\n",
    "# 纵深方向(3维) 完成组合操作，生成新的数组\n",
    "arr5 = np.dstack((arr1,arr2))\n",
    "print(arr5)\n",
    "print('%' * 30)\n",
    "\n",
    "# 纵深方向(3维) 完成拆分操作，生成新的数组\n",
    "x,y = np.dsplit(arr5,2)\n",
    "print(x)\n",
    "print(\"=\" * 30)\n",
    "print(y)\n"
   ],
   "id": "aafd6f2f4678af55",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 2 3]\n",
      " [4 5 6]]\n",
      "==============================\n",
      "[[ 7  8  9]\n",
      " [10 11 12]]\n",
      "$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n",
      "[[[ 1  7]\n",
      "  [ 2  8]\n",
      "  [ 3  9]]\n",
      "\n",
      " [[ 4 10]\n",
      "  [ 5 11]\n",
      "  [ 6 12]]]\n",
      "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n",
      "[[[1]\n",
      "  [2]\n",
      "  [3]]\n",
      "\n",
      " [[4]\n",
      "  [5]\n",
      "  [6]]]\n",
      "==============================\n",
      "[[[ 7]\n",
      "  [ 8]\n",
      "  [ 9]]\n",
      "\n",
      " [[10]\n",
      "  [11]\n",
      "  [12]]]\n"
     ]
    }
   ],
   "execution_count": 18
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "## 4. 多维数组组合与拆分的相关函数\n",
   "id": "ce02eefc63a56a28"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-26T03:26:18.906945Z",
     "start_time": "2025-08-26T03:26:18.893653Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 通过axis作为关键字参数指定组合的方向，取值如下：\n",
    "# 若待组合的数组都是二维数组：\n",
    "#   0: 垂直方向组合\n",
    "#   1: 水平方向组合\n",
    "# 若待组合的数组都是三维数组：\n",
    "#   0: 垂直方向组合\n",
    "#   1: 水平方向组合\n",
    "#   2: 深度方向组合\n",
    "print(arr1)\n",
    "print('=' * 30)\n",
    "print(arr2)\n",
    "print('=' * 30)\n",
    "\n",
    "k = np.concatenate((arr1, arr2), axis=1)\n",
    "print(k)\n",
    "print('=' * 30)\n",
    "# 通过给出的数组与要拆分的份数，按照某个方向进行拆分，axis的取值同上\n",
    "m,n = np.split(k, 2, axis=0)\n",
    "print(m)\n",
    "print(\"=\" * 30)\n",
    "print(n)\n"
   ],
   "id": "7cee27460184c931",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 2 3]\n",
      " [4 5 6]]\n",
      "==============================\n",
      "[[ 7  8  9]\n",
      " [10 11 12]]\n",
      "==============================\n",
      "[[ 1  2  3  7  8  9]\n",
      " [ 4  5  6 10 11 12]]\n",
      "==============================\n",
      "[[1 2 3 7 8 9]]\n",
      "==============================\n",
      "[[ 4  5  6 10 11 12]]\n"
     ]
    }
   ],
   "execution_count": 23
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "## 补充:复数数组的相关属性",
   "id": "d9870835d7180c9d"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-26T03:37:35.310567Z",
     "start_time": "2025-08-26T03:37:35.297842Z"
    }
   },
   "cell_type": "code",
   "source": [
    "arr6 = np.array([\n",
    "    [7+2j, 2+3j, 9+3j],\n",
    "    [3, 5+9j, 6+4j],\n",
    "    [36+5j,42+7j,33+2j]\n",
    "])\n",
    "\n",
    "print(arr6)\n",
    "print('数组维度:',arr6.shape)\n",
    "print('数组元素类型：',arr6.dtype)\n",
    "print('数组元素数量：',arr6.size)\n",
    "print('数组维度：',arr6.ndim)\n",
    "print('数组元素的字节数:',arr6.itemsize)\n",
    "print('数组元素的总字节数：',arr6.nbytes)\n",
    "print('数组复数实部的数是:',arr6.real)\n",
    "print('数组复数虚部的数是:',arr6.imag)"
   ],
   "id": "62cf763bea9ea34f",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 7.+2.j  2.+3.j  9.+3.j]\n",
      " [ 3.+0.j  5.+9.j  6.+4.j]\n",
      " [36.+5.j 42.+7.j 33.+2.j]]\n",
      "数组维度: (3, 3)\n",
      "数组元素类型： complex128\n",
      "数组元素数量： 9\n",
      "数组维度： 2\n",
      "数组元素的字节数: 16\n",
      "数组元素的总字节数： 144\n",
      "数组复数实部的数是: [[ 7.  2.  9.]\n",
      " [ 3.  5.  6.]\n",
      " [36. 42. 33.]]\n",
      "数组复数虚部的数是: [[2. 3. 3.]\n",
      " [0. 9. 4.]\n",
      " [5. 7. 2.]]\n"
     ]
    }
   ],
   "execution_count": 32
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
